package objects;

import gui.PlayerSkin;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import main.GameMain;
import network.GameNetwork;
import network.NetworkListener;




public class Player implements KeyListener, NetworkListener
{
   private final int	objId;
   private final PlayerSkin skin;

   public Player(int objId, int x, int y)
   {
      this.objId = objId;
      this.skin = new PlayerSkin(x, y);
      GameMain.addComponent(skin);
   }

   public int getObjId()
   {
      return objId;
   }

   @Override
   public void keyTyped(KeyEvent e)
   {}

   @Override
   public void keyReleased(KeyEvent e)
   {}

   @Override
   public void keyPressed(KeyEvent e)
   {
      try {
	 switch (e.getKeyCode())
	 {
	 case KeyEvent.VK_DOWN:
	    skin.setLocation(skin.getX(), skin.getY() + 5);
	    GameNetwork.sendPlayerMove(objId, skin.getBounds().x, skin.getBounds().y);
	    break;
	 case KeyEvent.VK_UP:
	    skin.setLocation(skin.getBounds().x, skin.getBounds().y - 5);
	    GameNetwork.sendPlayerMove(objId, skin.getBounds().x, skin.getBounds().y);
	    break;
	 case KeyEvent.VK_LEFT:
	    skin.setLocation(skin.getBounds().x - 5, skin.getBounds().y);
	    GameNetwork.sendPlayerMove(objId, skin.getBounds().x, skin.getBounds().y);
	    break;
	 case KeyEvent.VK_RIGHT:
	    skin.setLocation(skin.getBounds().x + 5, skin.getBounds().y);
	    GameNetwork.sendPlayerMove(objId, skin.getBounds().x, skin.getBounds().y);
	    break;
	 default:
	    break;
	 }

      } catch (Exception exc) {
	 exc.printStackTrace();
      }
   }

   @Override
   public void onPlayerMove(int objId, int x, int y)
   {
      skin.setLocation(x, y);
      skin.repaint();
   }

   @Override
   public void onPlayerLogin(int objId, int x, int y)
   {
      // TODO Auto-generated method stub
   }

}
